<template>
  <div class="permission">
    <el-card>
      <div style="text-align: right">
        <el-button
          type="primary"
          @click="addEvent(1, '0')"
        >+添加菜单</el-button>
      </div>
      <!-- 表格里要用树形列表必须要有row-key属性,且当前row必须有children -->
      <el-table :data="list" :props="{ label: 'name' }" row-key="id">
        <el-table-column prop="name" label="菜单名称" />
        <el-table-column prop="code" label="权限标识" />
        <el-table-column prop="description" label="权限描述" />
        <el-table-column width="400px" label="操作">
          <template v-slot="{ row }">
            <div>
              <el-button
                v-if="row.type === 1"
                type="text"
                @click="addEvent(1, row.id)"
              >添加权限页面</el-button>
              <el-button
                v-if="row.type === 1"
                type="text"
                @click="addEvent(2, row.id)"
              >添加权限按钮</el-button>
              <el-button
                type="text"
                @click="editEvent(row)"
              >编辑权限点</el-button>
              <el-button
                type="text"
                fixed="right"
                @click="delEvent(row.id)"
              >删除权限点</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <Add ref="add" v-model="isShow" />
  </div>
</template>

<script>
import { sysPermission, sysPermissionDelete } from '@/api/permissions'
import { changeData } from '@/utils/index'
import Add from './components/Add.vue'
export default {
  components: { Add },
  data() {
    return {
      list: [],
      isShow: false
    }
  },
  created() {
    this.getData()
  },
  methods: {
    // 获取权限列表
    async getData() {
      const res = await sysPermission()
      // console.log('获取所有权限', res)
      this.list = changeData(res.data, '0')
    },
    // 添加权限菜单
    addEvent(type, pid) {
      this.isShow = true
      this.$refs.add.form.type = type
      this.$refs.add.form.pid = pid
      this.$refs.add.mode = 'add'
    },
    // 删除权限
    delEvent(id) {
      this.$confirm('您确定要删除该权限吗?', '温馨提示')
        .then(async() => {
          await sysPermissionDelete(id)
          this.$message.success('删除权限成功!')
          this.isShow = false
          this.getData()
        })
        .catch(() => {})
    },
    // 编辑权限
    editEvent(row) {
      this.isShow = true
      this.$refs.add.form = JSON.parse(JSON.stringify(row))
      this.$refs.add.mode = 'edit'
    }
  }
}
</script>

<style lang="scss" scoped>
.permission {
  padding: 15px;
}
</style>
